<template>
	<view class="content">
		<!--轮播图部分-->
		<swiper class="box_swiper" circular indicator-dots autoplay interval="2000">
			<swiper-item><image src="https://tianweiwuye.oss-cn-beijing.aliyuncs.com/file/20240823131119652333fda7aec3a0b0/home1.jpg" class="swiper_img"></image></swiper-item>
			<swiper-item><image src="https://tianweiwuye.oss-cn-beijing.aliyuncs.com/file/20240823131404122334c361c64c0bd6/home2.jpg" class="swiper_img"></image></swiper-item>
			<swiper-item><image src="https://tianweiwuye.oss-cn-beijing.aliyuncs.com/file/202408231314513733354904ebd0465a/home3.jpg" class="swiper_img"></image></swiper-item>
			<swiper-item><image src="https://tianweiwuye.oss-cn-beijing.aliyuncs.com/file/20240823131532118336bafd4b864dfd/home4.jpg" class="swiper_img"></image></swiper-item>
		</swiper>
		<!--中间显示搜索出的门图标信息-->
		<view class="box_door">
			<!--每一个搜索到的上图片下文字容器-->
			<view class="door_item" v-for="(item,index) in array" :key="index">
				<view class="door_item_up" @click="opendoor(item.id)"><image :src="item.img" class="up_image"></image></view>
				<view class="down_item_down">{{item.name}}</view>
			</view>
			
			<!-- <view class="door_item">
				<view class="door_item_up"><image src='../../static/door/kk02.png' class="up_image"></image></view>
				<view class="down_item_down">A-3楼电梯间</view>
			</view>
			<view class="door_item">
				<view class="door_item_up"><image src='../../static/door/kk02.png' class="up_image"></image></view>
				<view class="down_item_down">A-3楼电梯间</view>
			</view> -->
		</view>
		<!--底部开门按钮区域-->
		<view class="box_bottom">
			<!-- <view><image src='../../static/door/dis_start.png' class="bottom_img"></image></view> 
			<view class="bottom_txt">未发现，点击搜索</view> -->
			<view><image :src="dis_img" class="bottom_img"></image></view>
			<!-- <view class="bottom_txt" v-if="status_txt.indexOf('点击搜索')!=-1" @click="updatedis_status">{{status_txt}}</view>
			<view class="bottom_txt" v-else>{{status_txt}}</view> -->
			<view class="bottom_txt">{{status_txt}}</view>
		</view>
	</view>
</template>

<script>
	import g_blegd from "@/utils/door/BLEGD.js";
	export default {
		data() {
			return {
				// g_egklist:[
				// 	{ name: "00:15:83:00:9C:54", key: "@03174225391805??*", address: "3楼A-B座" },
				// 	{ name: "00:15:83:00:9C:59", key: "@03174225381805??*", address: "2楼A-B座" },
				// 	{ name: "00:15:83:00:9C:4F", key: "@03174225371805??*", address: "临2楼A-B座" },
				// 	{ name: "00:15:83:00:9C:1B", key: "@03174225401805??*", address: "测试 上海" },
				// 	{ name: "00:15:83:00:9C:1E", key: "@03174225411805??*", address: "A-2楼电梯间" },
				// 	{ name: "00:15:83:00:9C:1D", key: "@03174225421805??*", address: "临3楼A-B座" },
					
				// 	{ name: "00:15:83:00:9D:0B", key: "@03D15115811805??*", address: "A-1楼电梯间" },
				// 	{ name: "00:15:83:00:9D:13", key: "@03F49B1AB21805??*", address: "B-1楼东门" },
				// 	{ name: "00:15:83:00:9C:AB", key: "@03437986ED1805??*", address: "A-3楼电梯间" },
				// 	{ name: "00:15:83:00:9C:F6", key: "@03B9164B141805??*", address: "A-4楼电梯间" },
				// 	{ name: "00:15:83:00:9C:F4", key: "@037B95E7F61805??*", address: "A-5楼电梯间" },
				// 	{ name: "00:15:83:00:9C:1C", key: "@03654224FB1805??*", address: "A-6楼电梯间" },
				// 	{ name: "00:15:83:00:9C:B4", key: "@0390EB21BD1805??*", address: "A-7楼电梯间" },
				// 	{ name: "00:15:83:00:9D:33", key: "@03C4F1FB581805??*", address: "A-8楼电梯间" },
				// 	{ name: "00:15:83:00:9D:22", key: "@036B919C531805??*", address: "A-9楼电梯间" },
				// 	{ name: "00:15:83:00:9C:E6", key: "@031F0AC4341805??*", address: "A-10楼电梯间" },
				// 	{ name: "00:15:83:00:9C:E5", key: "@031291E04A1805??*", address: "A-11楼电梯间" },
				// 	{ name: "00:15:83:00:9D:32", key: "@035A3AF1771805??*", address: "A-12楼电梯间" },
				// 	{ name: "00:15:83:00:9D:20", key: "@036105CEA01805??*", address: "C-3楼" },
				// 	{ name: "00:15:83:30:C1:27", key: "@032376ebc11801??*", address: "芳草园4-19" },
				// 	{ name: "00:15:83:00:9C:48", key: "@039005DD8A1805??*", address: "测试 014" },
				// 	{ name: "00:15:83:00:9C:3E", key: "@03454B75EC1805??*", address: "测试 015" },
				// 	{ name: "00:15:83:00:9C:3F", key: "@037C03D2C51805??*", address: "测试 016" },
				// 	{ name: "00:15:83:00:9C:40", key: "@033CE526541805??*", address: "测试 017" },
				// 	{ name: "00:15:83:00:9C:C0", key: "@03A68B21371805??*", address: "测试 018" },
				// 	{ name: "00:15:83:00:9B:F3", key: "@03C8FF833E1805??*", address: "C-4楼-02" },
				// 	{ name: "00:15:83:00:9C:A7", key: "@03DF959FBA1805??*", address: "测试 020" },
				// 	{ name: "00:15:83:00:9C:B9", key: "@03D6348B701805??*", address: "C-4楼-01" },
				// ], 
				g_egklist:[], //蓝牙门数组
				innerAudioContext:null, //音频组件
				dis_img:"/page_door/static/img/dis_start.png", //底部初始化的搜索按钮
				status_txt:"", //底部图片旁边的文字
				
				wx_code:"", //用于验证用户的code
				array:null,
				devid:null,
				start_dis_icon:"",
				
				//在此之上补充变量'
			}
		},
		onLoad() {
			//初始化音频组件
			this.innerAudioContext=wx.createInnerAudioContext();
			//底部图片初始化赋值
			this.start_dis_icon="/page_door/static/img/dis_start.png";
			
			this.devid = null;
			console.log("test...................")
			this.updatedis_status(false, "搜索蓝牙门禁中...");
			console.log("预置的蓝牙门禁数据==",this.g_egklist);
			var that=this;
			wx.login({
				success: res => {
					console.log("wx.login的返回值==", res)
					that.wx_code=res.code;
					wx.request({
						url: 'https://www.tianweiwuye.com/propAPP', 
						data:{
							code:res.code
						},
						method: 'GET',
						success: res => {
							console.log("wx.request success:", res.data);
							g_blegd.StartScan(8333, 1, res.data, function (info){ //原来代码中的参数是res.data
							//g_blegd.StartScan(8333, 1, that.g_egklist, function (info) { //最近提交使用的是这个参数 g_egklist
								console.log("调用StartScan函数返回值info==",info);
								wx.hideToast();
								if(info.code<200){
									wx.showToast({
										title: info.info,
										icon: 'loading',
										duration: 15000
									})
								}else if(info.code==200){
									that.innerAudioContext.src = "/page_door/static/img/2926.mp3";
									console.log("音频文件地址innerAudioContext.src==",that.innerAudioContext.src);
									that.innerAudioContext.play();
									wx.showToast({
										title: info.info,
										icon: 'success',
										duration: 1000
									})
								}else{
									wx.showToast({
										title: info.info,
										icon: 'none',
										duration: 1000
									})
								}
								if(info.content!="")
									that.status_txt=info.content;
							}.bind(that), function(blegdlist){
								console.log("137行打印参数blegdlist==",blegdlist);
								if(blegdlist.length==0){
									that.status_txt="未发现蓝牙门禁...";
									var arr=new Array();
									that.array=arr;
								}else{
									that.status_txt="发现蓝牙门禁...";
									var arr = [];
									console.log("发现的蓝牙门禁blegdlist==", blegdlist);
									for(var i=0;i<blegdlist.length;i++)
										arr.push({ id:blegdlist[i].device_id,name:blegdlist[i].address,img:"/page_door/static/img/kk02bak.png"});
									that.array=arr;
								}
							}.bind(that), function(blegdlist,devid,onoroff,isdis,info){
								console.log("***updateAll_status*onoroff*isdis*info***==",onoroff,isdis,info);

								if(devid!=null)
									that.devid=devid;
								if (onoroff)
									that.devid=null;

								var img_door = "/page_door/static/img/kk02bak.png";
								var img_cdoor = "/page_door/static/img/kk02bak.png";
								var img_discovery = "/page_door/static/img/dis_start.png";

								if(isdis==1)
									img_discovery = "/page_door/static/img/dis4.gif";
								else{
									if(blegdlist.length==0)
										info="未发现，点击搜索";
									else
										info="点击重新搜索";

									if(!onoroff){
										img_door="/page_door/static/img/kk01.png";
										img_cdoor="/page_door/static/img/kk03.png";
										img_discovery="/page_door/static/img/cancel.png";
										info="开门中, 所有按钮禁用";
									}
								}

								var arr=[];
								console.log("打印当前blegdlist==", blegdlist);
								for(var i = 0; i < blegdlist.length; i++)
									if (that.devid!=null && blegdlist[i].device_id==that.devid)
										arr.push({id:blegdlist[i].device_id,name:blegdlist[i].address,img:img_cdoor});
									else
										arr.push({id:blegdlist[i].device_id,name:blegdlist[i].address,img:img_door});
								that.array=arr;
								that.start_dis_icon=img_discovery;
								if (isdis!=-1)
									that.dis_img=img_discovery;
								if(info!="")
									that.status_txt=info;
							}.bind(that), that.updatedis_status);
						},
						fail: res=>{
							that.status_txt="获取权限失败，检查网络连接";
						}
					});
				},
				fail: res => {
					that.status_txt="微信登录失败，检查网络连接";
				}
			});//微信登录结束
		},
		methods: {
			updatedis_status: function(isdis,onoff,info){
				console.log("参数isdis与页面变量start_dis_icon==",isdis, this.start_dis_icon);
				if(isdis)
					this.dis_img="/page_door/static/img/dis4.gif";
				else if(onoff)
					this.dis_img=this.start_dis_icon;
				if(onoff)
					if(info!="")
						this.status_txt=info;
			},
			
			dis_ble: function (e) {
				g_blegd.RefreshBLTBTNTimer(5333, false);
			},
			
			//点击开门触发
			opendoor: function (id) {
				console.log("param:", id);
				wx.login({
					success: res => {
						wx.showToast({
							title: '开门中',
							icon: 'loading',
							duration: 15000
						})
				
						g_blegd.StopScan(id);
					}
				})
			},
			
			//在此之上补充方法
		}
	}
</script>

<style>
	@import '../../common/css/c_door.css'; 
</style>
